package com.navinfo.platform.trip.common.arithmetic;


import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.exception.RangeAssert;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.arithmetic.var.VarSpeedEconomic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;
import com.navinfo.platform.trip.common.vehicle.VehicleInfo;

/**
 * 青汽、一汽新增，经济车速算法<br>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计<br/>
 */
@Arithmetic(name = "经济车速算法")
public class SpeedEconomic extends AbstractArithmetic<VarSpeedEconomic> {

    @Override
    public void calculate() {
        logger.debug("进入经济车速算法.");
        VehicleInfo vehicleInfo = drivingData.getVehicleInfo();

        if(null != vehicleInfo){
            CommonData commonData = getCommonData();
            int speedKmh = commonData.getSpeedKmh();

            //速度有效性判断
            RangeAssert.speedKmhIsInTheRange(speedKmh);

            double economicSpeedLow = vehicleInfo.getEconomicSpeedLow();
            double economicSpeedHigh = vehicleInfo.getEconomicSpeedHigh();
            if(economicSpeedLow>0 && economicSpeedHigh>economicSpeedLow){
                VarSpeedEconomic var = getObj();
                //经济车速统计
                if(speedKmh>=economicSpeedLow && speedKmh<=economicSpeedHigh){
                    var.addMileage(commonData.getDffMileage());
                    var.addFuel(commonData.getDffFuel());
                    var.addDuration(commonData.getDffTime());
                }

                //车速大于min（90km/h，经济车速区间上限）且非滑行状态的里程统计
                if(speedKmh>Math.min(90, economicSpeedHigh) && commonData.getAcceleratorPosition()>0 ){
                    var.addNoCoastMileage(commonData.getDffMileage());
                }
            }
        }

        logger.debug("经济车速算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarSpeedEconomic var = super.getObj();
        statisticDataOut.setEconomicSpeedFuel(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setEconomicSpeedMileage(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setEconomicSpeedDuration(ArithmeticUtils.timeMsToSecond(var.getDuration()));
        statisticDataOut.setEconomicSpeedNoCoastMileage(ArithmeticUtils.mileageMToM(var.getNoCoastMileage()));
    }

}
